//**************************************************************** 
//* Copyright (c) 2004 Ford Motor Company. All Rights Reserved. 
//* 
//* $$Workfile:   FindEconomicIndexDefQuery.java  $$ 
//* $$Revision:   1.2  $$ 
//* $$Author:   JLOPE262  $$ 
//* $$Date:   Nov 24 2008 07:16:40  $$ 
//* 
//***************************************************************** 
package ar.com.ford.it.fullEconomics.persistence.dao.query;

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese01Economic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese02Econsource;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class FindEconomicIndexDefQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
            Object[] parameters) {
        Expression exp = null;
        Teese01Economic economicIndexDef = (Teese01Economic) parameters[0];
        String desc = economicIndexDef.getEese01EconNameN();
        Teese02Econsource sour = economicIndexDef.getEese01EconsourceNumberR();
        Teesg01Plant plant = economicIndexDef.getEese01PlantCodeC();
        Integer code = economicIndexDef.getEese01EconNumberK();
        desc = economicIndexDef.getEese01EconNameN();

        if (desc == null)
            desc = "%";
        else
            desc += "%";
        // Realizo la busqueda por codigo o descripcion de Indice Economico
        exp = builder.get("eese01PlantCodeC").equal(plant);
        if (code != null)
            exp = exp.and(builder.get("eese01EconNumberK").equal(economicIndexDef.getEese01EconNumberK()));
        if (sour != null)
            exp = exp.and(builder.get("eese01EconsourceNumberR").equal(sour));
        
        exp = exp.and(builder.get("eese01EconNameN").toLowerCase().like(
                desc.toLowerCase()));

        return exp;
    }

}
